package com.lanou3g.greendaodemo.entity.greendao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.zakerproject.base.MyApplication;
import com.lanou.zakerproject.subscribe.ChannelSearch;

/**
 * Created by Risky on 15/10/29.
 */
public class DaoSingleton {
    private static final String DATABASE_NAME = "daodemo.db";

    /**
     * 省略一些控件的声明
     */

    private DaoMaster daoMaster;   // 管理者
    private SQLiteDatabase db; // 数据库
    private DaoSession daoSession;   // 会话
    private Context context;
    private DaoMaster.DevOpenHelper helper;
    private NewsScrollEntityDao newsScrollEntityDao; // 对应的表,由java代码生成的,对数据库内相应的表操作使用此对象
    private GridViewEntityDao gridViewEntityDao;
    private NewsDataEntityDao newsDataEntityDao;


    private DaoSingleton() {
        context = MyApplication.getContext();
    }

    public static DaoSingleton getInstance() {
        return SingletonHolder.instance;
    }

    private static final class SingletonHolder {
        private static final DaoSingleton instance = new DaoSingleton();
    }

    private DaoMaster.DevOpenHelper getHelper() {
        if (helper == null) {
            helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
        }
        return helper;
    }

    private SQLiteDatabase getDb() {
        if (db == null) {
            db = getHelper().getWritableDatabase();
        }
        return db;
    }

    private DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            daoMaster = new DaoMaster(getDb());
        }
        return daoMaster;
    }

    private DaoSession getDaoSession() {
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }

    public NewsScrollEntityDao getRecommendDao() {
        if (newsScrollEntityDao == null) {
            newsScrollEntityDao = getDaoSession().getNewsScrollEntityDao();
        }
        return newsScrollEntityDao;
    }

    public GridViewEntityDao getGridViewEntityRecommendDao() {
        if (gridViewEntityDao == null) {
            gridViewEntityDao = getDaoSession().getGridViewEntityDao();
        }
        return gridViewEntityDao;
    }
    public NewsDataEntityDao getNewsDataEntityRecommendDao(){
        if (newsDataEntityDao == null){
            newsDataEntityDao = getDaoSession().getNewsDataEntityDao();
        }
        return newsDataEntityDao;
    }


}
